package edu.ccut.saturn.studio.logic.model.saturn;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.IObserver;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.dialog.saturndatamapping.SaturnDataMappingDialog;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.logic.figure.SaturnFigure;
import edu.ccut.saturn.studio.logic.model.Shape;

public class SaturnDataMapping extends Shape {

	public static final String KEY_NODE_MAPPING = Messages
			.getString("SaturnDataMapping.DataMapping"); //$NON-NLS-1$
	private static final String PROPERTY_FULLKEY = "property_fullkey";
	private static final String PROPERTY_VALUEKEY = "property_valuekey";
	private static final String PROPERTY_SATURN_MAPPINGRULES = "propterty_saturn_mappingrules";
	private static final long serialVersionUID = -5526540637833181798L;
	private static int number = 1;
	private List<SaturnMappingRule> saturnMappingRules = new ArrayList<SaturnMappingRule>();
	private List<SaturnMappingRule> saturnMappingSource = new ArrayList<SaturnMappingRule>();
	private Image image = Activator.getImage(Images.DATAMAP_ICON);
	private String valuekey = "";
	private String fullkey = "";

	public SaturnDataMapping() {
		super.setName(KEY_NODE_MAPPING + number++);
	}

	public Node doRefresh() {
		Node root = super.getRoot();

		Node saturndataMapping = SaturnW3CUtil.createChildNode(root,
				"saturndata-mapping", null);

		SaturnW3CUtil.createChildNode(saturndataMapping, "name", super.getName());
		Node target = SaturnW3CUtil.createChildNode(saturndataMapping, "target",
				null);
		SaturnW3CUtil.createAttribute(target, "value-key", this.getValuekey());
		SaturnW3CUtil.createAttribute(target, "fullkey", this.getFullkey());

		Node mappingrules = SaturnW3CUtil.createChildNode(saturndataMapping,
				"mapping-rules", null);

		refreshRules(mappingrules, this.getSaturnMappingRules());
		
		return saturndataMapping;
	}

	private void refreshRules(Node mappingrules,
			List<SaturnMappingRule> saturnMappingRules) {
		
		if (saturnMappingRules != null) {
			for (SaturnMappingRule saturnMappingRule : saturnMappingRules) {
				saturnMappingRule.createNode(mappingrules);
			}
		}
	}

	public Shape cloneShape(Point mousePoint, Shape shape) {
		return null;
	}

	public Dialog getDialog() {
		return new SaturnDataMappingDialog(super.getShell(), this);
	}

	public IFigure getFigure() {
		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(image);
		saturnFigure.setName(super.getName());
		return saturnFigure;
	}

	public Image getIcon() {
		return image;
	}

	protected void setImage(Image image) {
		this.image = image;
	}

	public List<SaturnMappingRule> getSaturnMappingRules() {
		return saturnMappingRules;
	}

	public void setSaturnMappingRules(List<SaturnMappingRule> saturnMappingRules) {
		if (saturnMappingRules.size() != this.getSaturnMappingRules().size()) {
			this.saturnMappingRules = saturnMappingRules;
			firePropertyChange(PROPERTY_SATURN_MAPPINGRULES, null,
					saturnMappingRules);
			return;
		}
		for (SaturnMappingRule newsaturnMappingRule : saturnMappingRules) {

			for (SaturnMappingRule oldsaturnMappingRule : this
					.getSaturnMappingRules()) {
				if (!newsaturnMappingRule.equals(oldsaturnMappingRule)) {
					this.saturnMappingRules = saturnMappingRules;
					firePropertyChange(PROPERTY_SATURN_MAPPINGRULES, null,
							saturnMappingRules);
				}
			}
		}
	}

	public String getValuekey() {
		return valuekey;
	}

	public void setValuekey(String valuekey) {
		if (!this.valuekey.equals(valuekey)) {
			this.valuekey = valuekey;
			firePropertyChange(PROPERTY_VALUEKEY, null, valuekey);
		}
	}

	public String getFullkey() {
		return fullkey;
	}

	public void setFullkey(String fullkey) {
		if (!this.fullkey.equals(fullkey)) {
			this.fullkey = fullkey;
			firePropertyChange(PROPERTY_FULLKEY, null, fullkey);
		}
	}

	public List<SaturnMappingRule> getSaturnMappingSource() {
		return saturnMappingSource;
	}

	public void setSaturnMappingSource(List<SaturnMappingRule> saturnMappingSource) {
		this.saturnMappingSource = saturnMappingSource;
	}
	
	public List<SearchResult> keyMapInWorkspace(IObserver observer) {
		
		List<SearchResult> list = null;
	
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInWorkspace("saturndata" , observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	public List<SearchResult> keyMapInProject(IObserver observer) {
		List<SearchResult> list = null;
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInProject(super.findTheProject(), "saturndata",
					observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Parameters createNodeHtml(String filePath, String[] tempnames) {

		List<Parameters> basicvaluse = new ArrayList<Parameters>();
		Map<String, List<?>> summm = new HashMap<String, List<?>>();
		List<SaturnMappingRule> parameters = getSaturnMappingRules();
		String name = getName();
		Parameters para = new Parameters();
		
		para.setName(getName());
		para.setId(getId());
		para.setDescription(getDescription());
		para.setKey(getFullkey());
		para.setTarget(getValuekey());
	 	basicvaluse.add(para);
	 	
		para.setInfourl( name + "-dataMapping.html");
		para.setInfoname( name);
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Logic.DataMapNode")); //$NON-NLS-1$
		para.setImagename("datamapping");
		summm.put("parameters", parameters);
		summm.put("basicvaluse", basicvaluse);
		String templatePath = "template/export/logic/datamapping";
		String Path = filePath + File.separator + name + "-dataMapping.html";
		FreemarkerUtils.writeTemplateToFile(templatePath, summm, Path);
		
		return para;
	}

}
